package com.sky.validation.util;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ValidationUtil {

    public static Map<String, Object> validate(Object o){
        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        Validator validator = vf.getValidator();
        Set<ConstraintViolation<Object>> set = validator.validate(o);
        Map<String, Object> map = new HashMap<>(2);
        for (ConstraintViolation<Object> constraintViolation : set) {
            map.put("code", "10001");
            map.put("msg", constraintViolation.getMessage());
            return map;
        }
        return map;
    }
}
